﻿using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Collections.ObjectModel;
using System.Windows.Input;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Input;
using System.Linq;
using System.Windows.Documents;
using System.Windows.Shapes;
using System.Windows.Media.Media3D;
using System.Collections;
using System.Data;
using System.Diagnostics;
namespace Data
{
    public class Triangle3D 
    {
        private Vector3D normal;
        private Point3D v1;
        private Point3D v2;
        private Point3D v3;
        public Double Zmax()
        {
            Double tam = 0;
            tam =(V1.Z>=V2.Z?V1.Z:V2.Z);
            tam=(tam>=V3.Z?tam:V3.Z);
            return tam;
        }
        public Double Zmin()
        {
            Double tam = 0;
            tam = (V1.Z <= V2.Z ? V1.Z : V2.Z);
            tam = (tam <= V3.Z ? tam : V3.Z);
            return tam;
        }
        public Double Ymax()
        {
            Double tam = 0;
            tam = (V1.Y >= V2.Y ? V1.Y : V2.Y);
            tam = (tam >= V3.Y ? tam : V3.Y);
            return tam;
        }
        public Double Ymin()
        {
            Double tam = 0;
            tam = (V1.Y <= V2.Y ? V1.Y : V2.Y);
            tam = (tam <= V3.Y ? tam : V3.Y);
            return tam;
        }
        public Double Xmin()
        {
            Double tam = 0;
            tam = (V1.X <= V2.X ? V1.X : V2.X);
            tam = (tam <= V3.X ? tam : V3.X);
            return tam;
        }
        public Double Xmax()
        {
            Double tam = 0;
            tam = (V1.X >= V2.X ? V1.X : V2.X);
            tam = (tam >= V3.X ? tam : V3.X);
            return tam;
        }

        public Triangle3D(Vector3D n,Point3D p1,Point3D p2,Point3D p3)
        {
           normal = n;
           p1.X = Math.Round(p1.X, 6); p1.Y = Math.Round(p1.Y, 6); p1.Z = Math.Round(p1.Z, 6);
           p2.X = Math.Round(p2.X, 6); p2.Y = Math.Round(p2.Y, 6); p2.Z = Math.Round(p2.Z, 6);
           p3.X = Math.Round(p3.X, 6); p3.Y = Math.Round(p3.Y, 6); p3.Z = Math.Round(p3.Z, 6);
           V1 = p1; V2 = p2; V3 = p3;
        }
        public Triangle3D() { }

        public Vector3D Normal
        {
            set { normal = value; }
            get { return normal; }
        }
        
        public Point3D V1
        {
            set { v1 = value; }
            get { return v1; }
        }
        public Point3D V2
        {
            set { v2 = value; }
            get { return v2; }
        }
        public Point3D V3
        {
            set { v3 = value; }
            get { return v3; }
        }
        public Point3D Getcenter()
        {
            Point3D center = new Point3D(0,0,0);
            center.X = Math.Round((this.V1.X + this.V2.X + this.V3.X) / 3);
            center.Y = Math.Round((this.V1.Y + this.V2.Y + this.V3.Y) / 3);
            center.Z = Math.Round((this.V1.X + this.V2.Z + this.V3.Z) / 3);
            return center;
        }
        public static Vector3D CalculateNormal(Point3D P0, Point3D P1, Point3D P2)
        {
            Vector3D v0 = new Vector3D(P1.X - P0.X, P1.Y - P0.Y, P1.Z - P0.Z);

            Vector3D v1 = new Vector3D(P2.X - P1.X, P2.Y - P1.Y, P2.Z - P1.Z);

            return Vector3D.CrossProduct(v0, v1);
        }
        public static void addPointCombined(Point3D point, MeshGeometry3D mesh, Vector3D normal)
        {
            bool found = false;

            int i = 0;

            foreach (Point3D p in mesh.Positions)
            {
                if (p.Equals(point))
                {
                    found = true;
                    mesh.TriangleIndices.Add(i);
                    mesh.Positions.Add(point);
                    mesh.Normals.Add(normal);
                    break;
                   
                }

                i++;
            }

            if (!found)
            {
                mesh.Positions.Add(point);
                mesh.TriangleIndices.Add(mesh.TriangleIndices.Count);
                mesh.Normals.Add(normal);
            }

        }
        public bool CheckShared(Point3D p)
        {
            if ((p == V1) || (p == V2) ||( p ==V3))
                return true;
            return false;
        }
        public Vector3D CalculateNormal()
        {
            Vector3D v0 = new Vector3D(V2.X - V1.X, V2.Y - V1.Y, V2.Z - V1.Z);

            Vector3D v1 = new Vector3D(V3.X - V2.X, V3.Y - V2.Y, V3.Z - V1.Z);

            return Vector3D.CrossProduct(v0, v1);
        }


    }
   
}
